package com.asion.gankapp.view.fragments;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.asion.gankapp.R;
import com.asion.gankapp.adapters.BaseViewPagerAdapter;
import com.asion.gankapp.utils.titanic.Titanic;
import com.asion.gankapp.utils.titanic.TitanicTextView;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {

    private ViewPager baseviewpager;
    private TabLayout basetablayout;
    private List<String> titles = setTitles();
    private int type = setType();
    private LinearLayout mLoding;
    private TitanicTextView tv;

    public abstract List<String> setTitles();

    public abstract int setType();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        baseviewpager = (ViewPager) view.findViewById(R.id.base_viewpager);
        basetablayout = (TabLayout) view.findViewById(R.id.base_tablayout);

        basetablayout.setupWithViewPager(baseviewpager);
        baseviewpager.setAdapter(new BaseViewPagerAdapter(getChildFragmentManager(), titles, type));
        mLoding = (LinearLayout) view.findViewById(R.id.loding);
        tv = (TitanicTextView) view.findViewById(R.id.my_text_view);
        new Titanic().start(tv);
    }

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter("baseFrag");
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(broadcastReceiver, intentFilter);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String over = intent.getStringExtra("over");
            switch (over) {
                case "over":
                    mLoding.setVisibility(View.GONE);
                    break;
            }
        }
    };
}
